﻿
@{
    ViewData["Title"] = "ProcuredUpdate";
    Layout = "~/Views/Home/Menu.cshtml";
}

<div style="font-size:20px;color:gray;margin:10px;padding:16px">
    采购申请修改
</div>
<form class="layui-form" action="">
    <div class="layui-form-item" style="margin-left:240px">
        <div class="layui-inline">
            <label class="layui-form-label">采购编号:</label>
            <div class="layui-input-block">
                <input type="text" name="ProcuredNumber" lay-verify="required" id="ProcuredNumber" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label" style="width:auto">采购原料编号:</label>
            <div class="layui-input-inline">
                <input type="text" name="FeedstockNumber" lay-verify="required" id="FeedstockNumber" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left:230px">
        <div class="layui-inline">
            <label class="layui-form-label" style="width:auto">采购原料名称:</label>
            <div class="layui-input-inline">
                <input type="text" name="FeedstockName" lay-verify="required" id="FeedstockName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline" style="margin-left:-18px">
            <label class="layui-form-label" style="width:auto">采购原料数量:</label>
            <div class="layui-input-inline">
                <input type="number" id="FeedstockNum" name="FeedstockNum" min="0" lay-affix="number" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left:230px">
        <div class="layui-inline">
            <label class="layui-form-label" style="width:auto">采购原料型号:</label>
            <div class="layui-col-md6">
                <select id="Feedstocktype" name="Feedstocktype" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select"></select>
            </div>
        </div>
        <div class="layui-inline" style="margin-left:-30px">
            <label class="layui-form-label" style="width:auto">采购原料尺寸:</label>
            <div class="layui-col-md6">
                <select id="FeedstockSize" name="FeedstockSize" lay-verify="required" lay-filter="mySelectFilter" class="dialog-select"></select>
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left:240px">
        <div class="layui-inline">
            <label class="layui-form-label">申请日期：</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="AddTime" name="AddTime" placeholder="请选择日期">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">采购预算:</label>
            <div class="layui-input-inline">
                <input type="text" name="ProcuredBudget" lay-verify="required" id="ProcuredBudget" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left:245px">
        <div class="layui-inline">
            <label class="layui-form-label" style="width:auto">采购批次号:</label>
            <div class="layui-input-inline">
                <input type="text" name="LotNumber" lay-verify="required" id="LotNumber" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label" style="width:auto">采购单价:</label>
            <div class="layui-input-inline">
                <input type="text" name="ProcessPrice" lay-verify="required" id="ProcessPrice" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    @* 付款方式 *@
    <div class="layui-form-item" style="margin-left:240px">
        <label class="layui-form-label">付款方式:</label>
        <div class="layui-inline">
            <input type="radio" name="PayMethods" value="定金+尾款" title="定金+尾款" checked>
            <input type="radio" name="PayMethods" value="全款" title="全款">
        </div>
    </div>
    <br />
    <div class="layui-form-item" style="margin-left:500px">
        <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
        <button type="reset" class="layui-btn layui-btn-primary">取消</button>
    </div>
</form>
@* <script src="~/js/procuredcg/procuredadd.js"></script> *@
<script>
    layui.use(function () {
        var laydate = layui.laydate;
        // 日期选择器
        laydate.render({
            elem: '#AddTime',
        });
    });
    
    $(function () {
        fantian(); WarehouseShow()
    });
    //型号、尺寸下拉框
    function WarehouseShow() {
        layui.use('form', function () {
            var form = layui.form;
            var date = ["M8", "M10", "M12", "M14", "M16", "M18", "M20"]
            //console.log(date)
            var options = '<option value="">请选择型号</option>'; // 添加默认选项
            $.each(date, function (index, item) {
                options += '<option value="' + item + '">' + item + '</option>';
            });
            $(".dialog-select#Feedstocktype").html(options);
            form.render('select'); // 重新渲染下拉框

            //采购原料尺寸
            var date1 = ["8mm", "10mm", "12mm", "14mm", "16mm", "18mm", "20mm"]
            //console.log(date1)
            var options = '<option value="">请选择尺寸</option>'; // 添加默认选项
            $.each(date1, function (index, item) {
                options += '<option value="' + item + '">' + item + '</option>';
            });
            $(".dialog-select#FeedstockSize").html(options);
            form.render('select'); // 重新渲染下拉框
        });
    }
    var obj1={
        ProcuredState:'',
        addName:'',
        addTime:''
    }
    //采购申请反填
    function fantian() {
        // debugger;
        layui.use('layer', function () {
            var form = layui.form;
            var procuredId = localStorage['procuredId']
            // 创建表格实例
            $.ajax({
                url: "/RawMaterial/ProcuredMondel",
                type: "get",
                data: {
                    Id: procuredId,
                },
                success: function (data) {
                    data = JSON.parse(data);
                    console.log(data.data);
                    // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                    $(data.data).each(function () {
                        $("#procuredId").val(this.procuredId)
                        $("#ProcuredNumber").val(this.procuredNumber)
                        $("#FeedstockNumber").val(this.feedstockNumber)
                        $("#FeedstockName").val(this.feedstockName)
                        $("#FeedstockNum").val(this.feedstockNum)
                        $("#Feedstocktype").val(this.feedstocktype)
                        $("#FeedstockSize").val(this.feedstockSize)
                        form.render('select')
                        var now = new Date(this.addTime);
                        var year = now.getFullYear()
                        var month = ("0" + now.getMonth() + 2).slice(-2);
                        var day = ("0" + now.getDate()).slice(-2);
                        console.log(year+"-"+month+"-"+day)
                        $("#AddTime").val(year + "-" + month + "-" + day);
                        $("#ProcuredBudget").val(this.procuredBudget)
                        var lspayMethods = this.payMethods
                        $("[name='PayMethods']").each(function () {
                            if (lspayMethods.toString() == $(this).val()) {
                                $(this).prop("checked", true);
                            }
                        })
                        $("#LotNumber").val(this.lotNumber)
                        $("#ProcessPrice").val(this.processPrice)
                        obj1.ProcuredState = this.procuredState
                        obj1.addName = this.addName
                        obj1.addTime = this.addTime
                    })
                }
            });
        })
    }
    //采购申请修改
    layui.use('layer', function () {
        var $ = layui.$;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;
        var table = layui.table;
        // 对弹层中的表单进行初始化渲染
        form.render();
        // 表单提交事件
        form.on('submit(demo-login)', function (data) {
            var field = data.field;
            console.log(field)
            var obj = {
                procuredId: localStorage['procuredId'],
                procuredNumber: field.ProcuredNumber,
                feedstockNumber: field.FeedstockNumber,
                feedstockName: field.FeedstockName,
                feedstockNum: field.FeedstockNum,
                feedstocktype: field.Feedstocktype,
                feedstockSize: field.FeedstockSize,
                procuredBudget: field.ProcuredBudget,
                lotNumber: field.LotNumber,
                procuredState: obj1.ProcuredState,
                processPrice: field.ProcessPrice,
                processActual: field.FeedstockNum * field.ProcessPrice,
                payMethods: field.PayMethods,
                addName: obj1.addName,
                addTime: field.AddTime
            }
            console.log(obj)
            console.log(obj1)
            $.ajax({
                url: '/RawMaterial/ProcuredUpdate',
                type: 'post',
                data: { dto:obj, token: localStorage.getItem("token") },
                success: function (res) {
                    var s = JSON.parse(res)
                    console.log(res)
                    if (s.code == 1) {
                        layer.alert("修改成功");
                        setTimeout(function () {
                            location.href = "/RawMaterial/ProcuredShow";
                        }, 3000);
                    }
                    else {
                        layer.alert("修改成功");
                    }
                }
            })
            return false; //阻止默认 form 跳转
        });

    });
</script>

